define(["../../Source/Core/BoundingSphere", "../../Source/Core/Cartesian2", "../../Source/Core/Cartesian3", "../../Source/Core/Cartographic", "../../Source/Core/Color", "../../Source/Core/ComponentDatatype", "../../Source/Core/defineProperties", "../../Source/Core/Geometry", "../../Source/Core/GeometryInstance", "../../Source/Core/Math", "../../Source/Core/PrimitiveType", "../../Source/Scene/Appearance", "../../Source/Scene/BlendingState", "../../Source/Scene/HorizontalOrigin", "../../Source/Scene/LabelStyle", "../../Source/Core/ScreenSpaceEventHandler", "../../Source/Core/ScreenSpaceEventType", "../../Source/Scene/Primitive", "../../Source/Scene/VerticalOrigin", "../../Source/DataSources/CallbackProperty", "../../Source/DataSources/PolylineGlowMaterialProperty", "../../Source/DataSources/PolylineOutlineMaterialProperty"], (function (e, t, i, s, o, n, r, a, l, h, u, c, d, p, g, D, S, y, f, w, v, E) {
  "use strict";

  function m(e) {
    var t = e.viewer;
    this.viewer = t, this.actis = e.actis, this.scene = t.scene, this._enable = !1, this.enable = !0, this.startDraw = !1, this.startPoint = new i(0, 0, 0), this.endPoint = new i(0, 0, 0), this.eMeasureStatus = !1, this.viewer.scene.globe.depthTestAgainstTerrain = !0;
    var n = this;
    m.prototype.getStartPoint3D = function () {
      return n.startPoint
    }, m.prototype.getEndPoint3D = function () {
      return n.endPoint
    }, m.prototype.getMidPoint3D = function () {
      if (3 == n.eMeasureStatus || 2 == n.eMeasureStatus) {
        var e = s.fromCartesian(n.startPoint),
          t = s.fromCartesian(n.endPoint);
        if (null != e && null != t) {
          if (t.height > e.height) {
            var o = h.toDegrees(e.longitude),
              r = h.toDegrees(e.latitude);
            return i.fromDegrees(o, r, t.height)
          }
          o = h.toDegrees(t.longitude), r = h.toDegrees(t.latitude);
          return i.fromDegrees(o, r, e.height)
        }
      }
    }, m.prototype.getPoint3D = function () {
      var e = [];
      if (3 == n.eMeasureStatus || 2 == n.eMeasureStatus) {
        var t = s.fromCartesian(n.startPoint),
          o = s.fromCartesian(n.endPoint);
        if (null != t && null != o)
          if (o.height > t.height) {
            var r = h.toDegrees(t.longitude),
              a = h.toDegrees(t.latitude);
            e.push(n.startPoint, i.fromDegrees(r, a, o.height), n.endPoint, n.startPoint)
          } else {
            r = h.toDegrees(o.longitude), a = h.toDegrees(o.latitude);
            e.push(n.startPoint, i.fromDegrees(r, a, t.height), n.endPoint, n.startPoint)
          }
      }
      return e
    }, this.startEllipsoid = t.entities.add({
      name: "",
      position: new w(this.getStartPoint3D, !1),
      point: {
        pixelSize: 5,
        color: o.RED,
        disableDepthTestDistance: 1e4
      }
    }), this.endEllipsoid = t.entities.add({
      name: "",
      position: new w(this.getEndPoint3D, !1),
      point: {
        pixelSize: 5,
        color: o.RED,
        disableDepthTestDistance: 1e4
      }
    }), this.midEllipsoid = t.entities.add({
      name: "",
      position: new w(this.getMidPoint3D, !1),
      point: {
        pixelSize: 5,
        color: o.RED,
        disableDepthTestDistance: 1e4
      }
    }), this.triangleLine = t.entities.add({
      name: "",
      polyline: {
        positions: new w(this.getPoint3D, !1),
        followSurface: !1,
        width: 3,
        material: new v({
          glowPower: .5,
          color: o.YELLOW
        }),
        depthFailMaterial: new E({
          color: o.RED,
          outlineWidth: 2,
          outlineColor: o.BLACK
        })
      }
    }), this.entity00 = t.entities.add({
      label: {
        show: !0,
        showBackground: !0,
        font: "30px Helvetica",
        fillColor: o.WHITE,
        outlineColor: o.BACK,
        outlineWidth: 5,
        style: g.FILL_AND_OUTLINE,
        horizontalOrigin: p.CENTER,
        verticalOrigin: f.BASELINE,
        disableDepthTestDistance: 1e4,
        scale: .5
      }
    }), this.entity0 = t.entities.add({
      label: {
        show: !0,
        showBackground: !0,
        font: "30px Helvetica",
        fillColor: o.WHITE,
        outlineColor: o.BACK,
        outlineWidth: 5,
        style: g.FILL_AND_OUTLINE,
        horizontalOrigin: p.CENTER,
        verticalOrigin: f.BASELINE,
        disableDepthTestDistance: 1e4,
        scale: .5
      }
    }), this.entity1 = t.entities.add({
      label: {
        show: !0,
        showBackground: !0,
        font: "30px Helvetica",
        fillColor: o.WHITE,
        outlineColor: o.BACK,
        outlineWidth: 5,
        style: g.FILL_AND_OUTLINE,
        horizontalOrigin: p.CENTER,
        verticalOrigin: f.BASELINE,
        disableDepthTestDistance: 1e4,
        scale: .5
      }
    }), this.entity2 = t.entities.add({
      label: {
        show: !0,
        showBackground: !0,
        font: "30px Helvetica",
        fillColor: o.WHITE,
        outlineColor: o.BACK,
        outlineWidth: 5,
        style: g.FILL_AND_OUTLINE,
        horizontalOrigin: p.CENTER,
        verticalOrigin: f.BASELINE,
        disableDepthTestDistance: 1e4,
        scale: .5
      }
    }), this.entity3 = t.entities.add({
      label: {
        show: !0,
        showBackground: !0,
        font: "30px Helvetica",
        fillColor: o.WHITE,
        outlineColor: o.BACK,
        outlineWidth: 5,
        style: g.FILL_AND_OUTLINE,
        horizontalOrigin: p.CENTER,
        verticalOrigin: f.BASELINE,
        disableDepthTestDistance: 1e4,
        scale: .5
      }
    });
    var r = this.handler = new D(this.scene.canvas),
      a = void 0;
    r.setInputAction((function (e) {
      a = e.position
    }), S.LEFT_DOWN), r.setInputAction((function (e) {
      var t = Math.abs(a.x - e.position.x),
        i = Math.abs(a.y - e.position.y);
      t > 3 || i > 3 ? a = void 0 : n.addPoint3D(e)
    }), S.LEFT_UP), r.setInputAction((function (e) {
      n && n.enable && n.addPointTemp(e)
    }), S.MOUSE_MOVE)
  }
  return r(m.prototype, {
    enable: {
      get: function () {
        return this._enable
      },
      set: function (e) {
        this._enable !== e && (this._enable = e)
      }
    }
  }), m.prototype.addPoint3D = function (e) {
    if (this._enable && 3 != this.eMeasureStatus) {
      if (0 == this.eMeasureStatus) {
        this.eMeasureStatus = 1;
        var t = null;
        if (this.scene.pick(e.position)) t = this.scene.pickPosition(e.position);
        else {
          var i = this.scene.camera.getPickRay(e.position);
          t = this.scene.globe.pick(i, this.viewer.scene)
        }
        if (null == t) return;
        this.startPoint = t
      }
      2 == this.eMeasureStatus && (this.eMeasureStatus = 3), 3 == this.eMeasureStatus && (this.computeDistance(), this._enable = !1)
    }
  }, m.prototype.addPointTemp = function (e) {
    if (this._enable && (1 == this.eMeasureStatus && (this.endEllipsoid.show = !0, this.startEllipsoid.show = !0, this.midEllipsoid.show = !0, this.entity1.show = !0, this.entity2.show = !0, this.entity3.show = !0, this.triangleLine.show = !0, this.eMeasureStatus = 2), 2 == this.eMeasureStatus)) {
      var t = null;
      if (this.scene.pick(e.endPosition)) t = this.scene.pickPosition(e.endPosition);
      else {
        var i = this.scene.camera.getPickRay(e.endPosition);
        t = this.scene.globe.pick(i, this.viewer.scene)
      }
      if (null == t) return;
      null != t && (this.endPoint = t), this.eMeasureStatus = 2, this.computeDistance()
    }
    this.actis(this)
  }, m.prototype.computeDistance = function () {
    var e = s.fromCartesian(this.endPoint),
      t = h.toDegrees(e.longitude),
      o = h.toDegrees(e.latitude),
      n = e.height,
      r = s.fromCartesian(this.startPoint),
      a = h.toDegrees(r.longitude),
      l = h.toDegrees(r.latitude),
      u = r.height,
      c = i.distance(this.startPoint, i.fromDegrees(a, l, n)),
      d = i.distance(this.endPoint, i.fromDegrees(a, l, n)),
      p = i.distance(this.startPoint, this.endPoint),
      g = 180 * Math.asin(d / p) / Math.PI;
    n > u ? (this.entity00.position = i.fromDegrees(t, o, n), this.entity0.position = i.fromDegrees(a, l, u), this.entity1.position = i.fromDegrees(a, l, (u + n) / 2), this.entity2.position = i.fromDegrees((a + t) / 2, (l + o) / 2, n)) : (this.entity1.position = i.fromDegrees(t, o, (u + n) / 2), this.entity2.position = i.fromDegrees((a + t) / 2, (l + o) / 2, u)), this.entity00.label.show = !0, this.entity00.label.text = (90 - g).toFixed(2) + " °", this.entity0.label.show = !0, this.entity0.label.text = g.toFixed(2) + " °", this.entity1.label.show = !0, this.entity1.label.text = c.toFixed(2) + " m", this.entity2.label.show = !0, this.entity2.label.text = d.toFixed(2) + " m", this.entity3.position = i.fromDegrees((a + t) / 2, (l + o) / 2, (n + u) / 2), this.entity3.label.show = !0, this.entity3.label.text = p.toFixed(2) + " m"
  }, m.prototype.destroy = function () {
    this._enable = !1, this.endEllipsoid.show = !1, this.startEllipsoid.show = !1, this.midEllipsoid.show = !1, this.entity1.show = !1, this.entity2.show = !1, this.entity3.show = !1, this.triangleLine.show = !1, this.eMeasureStatus = 0, this.viewer.scene.globe.depthTestAgainstTerrain = !1, this.viewer.entities.remove(this.endEllipsoid), this.viewer.entities.remove(this.startEllipsoid), this.viewer.entities.remove(this.midEllipsoid), this.viewer.entities.remove(this.entity00), this.viewer.entities.remove(this.entity0), this.viewer.entities.remove(this.entity1), this.viewer.entities.remove(this.entity2), this.viewer.entities.remove(this.entity3), this.viewer.entities.remove(this.triangleLine)
  }, m
}));
